using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class FightController : MonoSingleton<FightController>
{
    public List<CharacterDataHolder> playerDataHolder=new();
    public List<CharacterDataHolder> enemyDataHolder=new();
    public List<EnemyAttackDataHolder> enemyAttackDataHoulder=new();
    private readonly WaitForSeconds _delaySecond = new WaitForSeconds(0.5f);
    public bool isStartFight;
    public bool isCheckPlayerWin;

    public void StartFight()
    {
        TurnController.Instance.StartTurn();
        isStartFight = true;
        isCheckPlayerWin = false;
        foreach (var item in enemyAttackDataHoulder)
        {
            item.SetNextTurnAttack(TurnController.Instance.turnCount);
        }
    }

    private void EnemyFight()//没有等待输入
    {
        if (!playerDataHolder.All(p => p.characterData.isDead()) && !enemyDataHolder.All(p => p.characterData.isDead()) && TurnController.Instance.isEnemyTurn)
        {
            StartCoroutine(EnemyAttack());
            TurnController.Instance.LeaveEnemyTurn();
        }
        else
        {
            if (playerDataHolder.All(p => p.characterData.isDead()))
            {
                Debug.Log("player lose");
                //TODO lose
            }
            else
            {
                Debug.Log("player win");
                //TODO win
            }
        }
    }

    private IEnumerator EnemyAttack()
    {
        for (int i = 0; i < enemyDataHolder.Count; i++)
        {
            if(!enemyDataHolder[i].characterData.isDead())
            {
                enemyAttackDataHoulder[i].DoAttack(i, TurnController.Instance.turnCount);
                if (!playerDataHolder.All(p => p.characterData.isDead()) && !enemyDataHolder.All(p => p.characterData.isDead()))
                {
                    yield return _delaySecond;
                }
            }
        }
        for (int i = 0; i < enemyDataHolder.Count; i++)
        {
            if (!enemyDataHolder[i].characterData.isDead())
            {
                enemyAttackDataHoulder[i].SetNextTurnAttack(TurnController.Instance.turnCount + 1);
            }
        }
        foreach (var item in playerDataHolder)
        {
            item.RemoveDefenceEveryTurn();
        }
        
        TurnController.Instance.ToPlayerTurn();
    }

    private void Fight()
    {
        if(TurnController.Instance.isPlayerTurn)
        {
            if(TurnController.Instance.isWaitForPlayer)
            {
                //Debug.Log("wait player");
            }
            else
            {
                TurnController.Instance.LeavePlayerTurn();
                TurnController.Instance.ToEnemyTurn();
                foreach (var item in enemyDataHolder)
                {
                    item.RemoveDefenceEveryTurn();
                }
            }
        }
        else if(TurnController.Instance.isEnemyTurn)
        {
            EnemyFight();
        }
    }

    public void EnemyOnDie(CharacterDataHolder enemy)
    {
        if(enemy.CharacterID!=0)
        {
            enemy.gameObject.GetComponent<EnemyAttackDataHolder>().preview.HideCurrentPreview();
        }
    }

    private void Update()
    {
        if(isStartFight)
        {
            Fight();
        }
        if(isCheckPlayerWin)
        {
            if (playerDataHolder.All(p => p.characterData.isDead()))
            {
                Debug.Log("player lose");
                //TODO lose
            }
            else if(enemyDataHolder.All(p=>p.characterData.isDead()))
            {
                Debug.Log("player win");
                //TODO win
            }
            isCheckPlayerWin = false;
        }
    }
}
